﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using EOL;
using BPO;
namespace PlotBookingRegistration
{
    public partial class Registration : Form
    {
        private BPO.BusinessRules _br = null;
        BackgroundWorker bgEmployee = null;
        private int EmployeeId;
        private int CustomerId;
        private int AgentId;
        public Security SecInstanc { get { return Security.getInstance(); } }
        public Registration()
        {
            InitializeComponent();
            ImplementSecurity();
        }

        private void ImplementSecurity()
        {
            if (SecInstanc.IsUserLoggedIn && SecInstanc.UserRole != UserRoles.BranchManager)
            {
                customerReToolStripMenuItem.Enabled = false;
                agentToolStripMenuItem.Enabled = false;
                tbRegistration.SelectedTab = tbEmployeeReg;
            }
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Hide();
            new HOME().Show();
        }

        private void printingToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Hide();
            new Printing().ShowDialog();
        }

        private void customerReToolStripMenuItem_Click(object sender, EventArgs e)
        {
            tbRegistration.SelectedTab = tbCustomerRegistration;
        }

        private void agentToolStripMenuItem_Click(object sender, EventArgs e)
        {
            tbRegistration.SelectedTab = tbAgentReg;
        }

        private void employeeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            tbRegistration.SelectedTab = tbEmployeeReg;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you sure to save the data?? ", "Save Data", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
            {
                SaveCustomer();
                MessageBox.Show("Data saved successfully. CustomerId is " + CustomerId.ToString("D5"));
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you sure to cancel. All unsaved changes will be lossed?? ", "Save Data", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
            {
                foreach (Control item in tbCustomerRegistration.Controls)
                {
                    if (item is GroupBox)
                    {
                        foreach (var grpControls in item.Controls)
                        {
                            if (grpControls is TextBox)
                                ((TextBox)grpControls).Clear();
                        }
                    }
                }
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you sure to save the data?? ", "Save Data", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
            {
                MessageBox.Show("Data edited successfully");
            }
        }

        private void tbAgentReg_Click(object sender, EventArgs e)
        {

        }

        private void Registration_Load(object sender, EventArgs e)
        {
            try
            {
                LoadDropDowns();
                SetComboReadOnly();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void SetComboReadOnly()
        {

            //ManageControls(this.Controls);
        }

        private void ManageControls(Control.ControlCollection ctrls)
        {
            foreach (Control item in this.Controls)
            {
                if (item is ComboBox)
                {
                    ((ComboBox)item).DropDownStyle = ComboBoxStyle.DropDownList;

                }
                if (item.HasChildren)
                    ManageControls(item.Controls);
            }
        }

        private void LoadDropDowns()
        {
            BindAgeDropDown();
            BindPlanDropDown();
            BindRelationDropDown();
            BindDesignation();
            BindRankDropDown();
            BindTermDropdown();
        }

        private void BindDesignation()
        {
            BPO.Utility.BindDropDown(cboDesignation, "Designation", "A");
        }

        private void BindRelationDropDown()
        {
            BPO.Utility.BindDropDown(cboRelation, "Relation", "A");

        }

        private void BindTermDropdown()
        {
            cbpTerm.DataSource = BPO.StaticData.GetAge(1, 20);
            cbpTerm.DisplayMember = "Age";
            cbpTerm.ValueMember = "Age";
        }

        private void BindRankDropDown()
        {
            BPO.Utility.BindDropDown(cboARank, "RNK", "M");
            cboARank.SelectedIndex = 0;
            cboARank.Enabled = false;
            BPO.Utility.BindDropDown(cboParentRank, "RNK", "M");

            BPO.Utility.BindDropDown(cboParentId, "PID", "M");

        }

        private void BindPlanDropDown()
        {
            DataTable dt = BPO.DynamicData.GetMasterData("PLN");
            cboPlan.DataSource = dt;
            cboPlan.DisplayMember = dt.Columns[1].ColumnName.ToString();
            cboPlan.ValueMember = dt.Columns[0].ColumnName.ToString();
        }

        private void BindAgeDropDown()
        {
            cboCustAge.DataSource = BPO.StaticData.GetAge(0, 100);
            cboCustAge.DisplayMember = "Age";
            cboCustAge.ValueMember = "Age";

            cboCNAge.DataSource = BPO.StaticData.GetAge(0, 100);
            cboCNAge.DisplayMember = "Age";
            cboCNAge.ValueMember = "Age";

            cboAAge.DataSource = BPO.StaticData.GetAge(0, 100);
            cboAAge.DisplayMember = "Age";
            cboAAge.ValueMember = "Age";

            cboANAge.DataSource = BPO.StaticData.GetAge(0, 100);
            cboANAge.DisplayMember = "Age";
            cboANAge.ValueMember = "Age";

            cboEAge.DataSource = BPO.StaticData.GetAge(0, 100);
            cboEAge.DisplayMember = "Age";
            cboEAge.ValueMember = "Age";

            cboExp.DataSource = BPO.StaticData.GetAge(0, 60);
            cboExp.DisplayMember = "Age";
            cboExp.ValueMember = "Age";
        }

        private void SaveCustomer()
        {
            EOL.ICustomer cust = new Customer();
            cust.CustomerId = CustomerId;
            cust.CBranch = txtCustBranch.Text;
            cust.CDate = dtCustomer.Value;
            cust.CustomerName = txtApplName.Text;
            cust.GuardianName = txtApplSO.Text;
            cust.CAge = Convert.ToInt32(cboCustAge.Text);
            cust.CAddress = txtAppladdress.Text;
            cust.AgentId = Convert.ToInt32(cboAgencyCode.SelectedValue);
            cust.PlanId = Convert.ToInt32(cboPlan.SelectedValue);
            cust.Term = Convert.ToInt32(cbpTerm.SelectedValue);
            cust.CNomineeName = txtNomName.Text;
            cust.CNGuardianName = txtNomSO.Text;
            cust.CNAddress = txtnomAddress.Text;
            cust.CNAge = Convert.ToInt32(cboCNAge.SelectedValue.ToString());
            cust.RelationKey = cboRelation.SelectedValue.ToString();
            cust.Amount = Convert.ToDecimal(txtPlanAmt.Text);
            BPO.BusinessRules br = new BPO.BusinessRules();
            br.SaveCustomerDetails((EOL.Customer)cust);
            CustomerId = cust.CustomerId;

        }

        private void btnSaveEmployee_Click(object sender, EventArgs e)
        {
            try
            {
                bgEmployee = new BackgroundWorker();
                bgEmployee.DoWork += bgEmployee_DoWork;

                EOL.IEmployee emp = new EOL.Employee();
                emp.EmployeeId = EmployeeId;
                emp.Age = Convert.ToInt32(cboEAge.Text.Trim());
                emp.EmployeeName = txtEmpName.Text;
                emp.FatherName = txtFName.Text;
                emp.MotherName = txtMName.Text;
                emp.Qualification1 = txtQual1.Text;
                emp.Qualification2 = txtQual2.Text;
                emp.Salary = Convert.ToInt64(txtEmpSalary.Text);
                emp.EmployeeAddress = txtEmpAddress.Text;
                emp.Designation = cboDesignation.SelectedValue.ToString();
                emp.JoiningDate = dtpJoiningdate.Value;
                emp.Experience = Convert.ToInt32(cboExp.Text);
                emp.PreviousCompany = txtPreCompany.Text;
                BPO.BusinessRules br = new BPO.BusinessRules();
                br.SaveEmployeeDetails((EOL.Employee)emp);
                EmployeeId = emp.EmployeeId;
                MessageBox.Show("Data Saved Successfully. Employee Id is " + emp.EmployeeId.ToString("D5"));
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        void bgEmployee_DoWork(object sender, DoWorkEventArgs e)
        {

        }

        private void btnSaveAgent_Click(object sender, EventArgs e)
        {
            try
            {
                EOL.IAgent _agent = new Agent();
                _agent.AgentId = AgentId;
                _agent.ABranch = txtABranch.Text;
                _agent.ADate = dtpADate.Value;
                _agent.AgentName = txtAgentName.Text;
                _agent.AGuardianName = txtACareOf.Text;
                _agent.AAge = Convert.ToInt32(cboAAge.Text);
                _agent.AAddress = txtAAddress.Text;
                _agent.RankId = Convert.ToInt32(cboARank.SelectedValue);
                _agent.ANomineeName = txtANomineeName.Text;
                _agent.ANGuardianName = txtANSonOf.Text;
                _agent.ANAddress = txtnomAddress.Text;
                _agent.ANAge = Convert.ToInt32(cboANAge.SelectedValue.ToString());
                _agent.ParentId = Convert.ToInt32(cboParentId.SelectedValue);
                BPO.BusinessRules br = new BPO.BusinessRules();
                br.SaveAgent((EOL.Agent)_agent);
                AgentId = _agent.AgentId;
                MessageBox.Show("Data Saved Successfully. Agent Number is " + AgentId.ToString("D5"));
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void tbCustomerRegistration_Click(object sender, EventArgs e)
        {

        }

        private void tbRegistration_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (SecInstanc.UserRole != UserRoles.BranchManager && (tbRegistration.SelectedTab == tbCustomerRegistration || tbRegistration.SelectedTab == tbAgentReg))
            {
                MessageBox.Show("You are not allowed to view this page");
                tbRegistration.SelectedTab = tbEmployeeReg;
            }
        }

        private void cboParentId_SelectedIndexChanged(object sender, EventArgs e)
        {
            string id = cboParentId.SelectedValue.ToString();
            int rank = BPO.BusinessRules.GetRank(Convert.ToInt32(id));
            cboParentRank.SelectedValue = rank.ToString();
        }


    }
}
